package com.study.hot.architecture.mvx.mvvm.viewmodel;

import androidx.databinding.ObservableArrayMap;
import androidx.databinding.ObservableField;

import com.study.hot.architecture.mvx.mvvm.model.Board;
import com.study.hot.architecture.mvx.mvvm.model.Player;


/**
 * <pre>
 *     author : slx
 *     time   : 2020/11/21
 *     desc   :
 * </pre>
 */
public class JZQViewModel {
    private Board model;
    // key：格子tag value：玩家姓名
    public ObservableArrayMap<String, String> cells = new ObservableArrayMap<>();
    // 胜利者
    public ObservableField<String> winner = new ObservableField<>();

    public JZQViewModel() {
        this.model = new Board();
    }

    // cell点击
    public void onCellSelected(int row, int col) {
        Player mark = model.mark(row, col);
        if (mark != null) {
            // 存放点击该cell的玩家
            cells.put("" + row + col, mark.toString());
            // 是否有胜利者
            winner.set(model.getWinner() == null ? null : model.getWinner().toString());
        }
    }

    // 重置
    public void onRestartSelected() {
        model.restart();
        winner.set(null);
        cells.clear();
    }
}
